Load all required libraries.

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)

Read in raw data from RDS.

raw_data <- readRDS("./n1_n2_cleaned_cases.rds")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
        p2

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.25, n = 730)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 13.02081 13.01763 13.01450 13.01141 13.00837 13.00537 13.00240 12.99948
##   [9] 12.99658 12.99372 12.99089 12.98808 12.98529 12.98253 12.97978 12.97705
##  [17] 12.97434 12.97163 12.96894 12.96625 12.96356 12.96087 12.95818 12.95549
##  [25] 12.95279 12.95008 12.94736 12.94463 12.94188 12.93911 12.93631 12.93350
##  [33] 12.93065 12.92778 12.92487 12.92193 12.91896 12.91594 12.91289 12.90981
##  [41] 12.90671 12.90358 12.90044 12.89728 12.89412 12.89095 12.88777 12.88460
##  [49] 12.88144 12.87829 12.87516 12.87204 12.86895 12.86589 12.86285 12.85986
##  [57] 12.85690 12.85399 12.85112 12.84831 12.84555 12.84285 12.84022 12.83766
##  [65] 12.83517 12.83275 12.83042 12.82817 12.82601 12.82394 12.82197 12.82010
##  [73] 12.81833 12.81668 12.81514 12.81371 12.81241 12.81123 12.80996 12.80840
##  [81] 12.80655 12.80444 12.80208 12.79949 12.79670 12.79372 12.79056 12.78725
##  [89] 12.78380 12.78024 12.77657 12.77282 12.76901 12.76515 12.76126 12.75736
##  [97] 12.75347 12.74961 12.74579 12.74203 12.73836 12.73478 12.73132 12.72799
## [105] 12.72481 12.72181 12.71900 12.71639 12.71400 12.71186 12.70998 12.70838
## [113] 12.70708 12.70609 12.70544 12.70513 12.70520 12.70566 12.70651 12.70780
## [121] 12.70976 12.71260 12.71624 12.72061 12.72564 12.73127 12.73742 12.74402
## [129] 12.75100 12.75828 12.76581 12.77351 12.78130 12.78912 12.79689 12.80456
## [137] 12.81204 12.81926 12.82616 12.83266 12.83869 12.84419 12.85070 12.85968
## [145] 12.87088 12.88403 12.89889 12.91520 12.93273 12.95120 12.97039 12.99002
## [153] 13.00985 13.02964 13.04912 13.06805 13.08617 13.10324 13.11900 13.13321
## [161] 13.14561 13.15594 13.16397 13.17124 13.17945 13.18850 13.19832 13.20881
## [169] 13.21991 13.23151 13.24354 13.25592 13.26857 13.28139 13.29431 13.30724
## [177] 13.32011 13.33282 13.34529 13.35745 13.36920 13.38046 13.39116 13.40120
## [185] 13.41051 13.41900 13.42658 13.43318 13.43871 13.44309 13.44623 13.44805
## [193] 13.44847 13.44741 13.44478 13.44049 13.43448 13.42681 13.41759 13.40695
## [201] 13.39498 13.38181 13.36756 13.35234 13.33626 13.31944 13.30200 13.28405
## [209] 13.26570 13.24708 13.22829 13.20946 13.19069 13.17211 13.15382 13.13595
## [217] 13.11861 13.09913 13.07518 13.04736 13.01629 12.98255 12.94676 12.90953
## [225] 12.87145 12.83314 12.79519 12.75822 12.72282 12.68961 12.65918 12.63215
## [233] 12.60912 12.58725 12.56346 12.53798 12.51104 12.48285 12.45365 12.42366
## [241] 12.39312 12.36224 12.33126 12.30039 12.26988 12.23994 12.21080 12.18269
## [249] 12.15584 12.13046 12.10680 12.08507 12.06550 12.04832 12.03355 12.02091
## [257] 12.01023 12.00132 11.99399 11.98806 11.98335 11.97966 11.97682 11.97464
## [265] 11.97292 11.97150 11.97017 11.96876 11.96708 11.96495 11.96218 11.95858
## [273] 11.95397 11.94816 11.94097 11.93372 11.92774 11.92291 11.91909 11.91614
## [281] 11.91394 11.91235 11.91122 11.91044 11.90986 11.90936 11.90878 11.90802
## [289] 11.90691 11.90534 11.90317 11.90027 11.89649 11.89171 11.88579 11.87860
## [297] 11.87064 11.86254 11.85430 11.84593 11.83745 11.82887 11.82020 11.81145
## [305] 11.80264 11.79377 11.78486 11.77592 11.76697 11.75801 11.74906 11.74013
## [313] 11.73123 11.72237 11.71357 11.70323 11.68995 11.67404 11.65584 11.63566
## [321] 11.61381 11.59063 11.56642 11.54151 11.51621 11.49085 11.46574 11.44121
## [329] 11.41757 11.39514 11.37425 11.35521 11.33833 11.32395 11.31238 11.30393
## [337] 11.29894 11.29569 11.29235 11.28899 11.28572 11.28262 11.27978 11.27729
## [345] 11.27524 11.27371 11.27281 11.27262 11.27322 11.27471 11.27718 11.28072
## [353] 11.28542 11.29136 11.29864 11.30734 11.31757 11.33022 11.34601 11.36468
## [361] 11.38597 11.40965 11.43546 11.46314 11.49246 11.52316 11.55500 11.58771
## [369] 11.62106 11.65479 11.68866 11.72241 11.75580 11.78857 11.82047 11.85126
## [377] 11.88069 11.90850 11.93445 11.95829 11.98304 12.01161 12.04351 12.07827
## [385] 12.11543 12.15451 12.19505 12.23658 12.27862 12.32070 12.36236 12.40312
## [393] 12.44251 12.48007 12.51532 12.54780 12.57703 12.60254 12.62386 12.64339
## [401] 12.66373 12.68472 12.70622 12.72807 12.75010 12.77218 12.79415 12.81585
## [409] 12.83712 12.85782 12.87778 12.89686 12.91490 12.93175 12.94725 12.96124
## [417] 12.97358 12.98411 12.99268 12.99912 13.00294 13.00389 13.00220 12.99813
## [425] 12.99191 12.98379 12.97400 12.96279 12.95040 12.93707 12.92305 12.90858
## [433] 12.89389 12.87924 12.86485 12.85098 12.83787 12.82576 12.81488 12.80549
## [441] 12.79782 12.78964 12.77875 12.76542 12.74991 12.73250 12.71346 12.69306
## [449] 12.67158 12.64929 12.62646 12.60336 12.58026 12.55744 12.53517 12.51372
## [457] 12.49335 12.47436 12.45700 12.44154 12.42827 12.41745 12.40711 12.39529
## [465] 12.38221 12.36813 12.35328 12.33792 12.32227 12.30660 12.29113 12.27612
## [473] 12.26181 12.24843 12.23624 12.22547 12.21638 12.20919 12.20308 12.19705
## [481] 12.19115 12.18541 12.17986 12.17456 12.16952 12.16481 12.16044 12.15646
## [489] 12.15290 12.14981 12.14722 12.14517 12.14369 12.14283 12.14262 12.14311
## [497] 12.14432 12.14629 12.14907 12.15337 12.15977 12.16810 12.17820 12.18992
## [505] 12.20309 12.21755 12.23314 12.24969 12.26705 12.28505 12.30353 12.32233
## [513] 12.34128 12.36024 12.37902 12.39748 12.41544 12.43276 12.44926 12.46478
## [521] 12.47917 12.49226 12.50389 12.51390 12.52212 12.53021 12.53981 12.55078
## [529] 12.56297 12.57624 12.59043 12.60539 12.62099 12.63706 12.65346 12.67005
## [537] 12.68667 12.70318 12.71943 12.73527 12.75055 12.76512 12.77884 12.79156
## [545] 12.80312 12.81339 12.82220 12.82943 12.83491 12.83849 12.84004 12.83940
## [553] 12.83642 12.83145 12.82504 12.81733 12.80843 12.79851 12.78767 12.77607
## [561] 12.76383 12.75109 12.73799 12.72466 12.71123 12.69784 12.68463 12.67172
## [569] 12.65926 12.64508 12.62717 12.60589 12.58164 12.55477 12.52565 12.49467
## [577] 12.46218 12.42857 12.39420 12.35945 12.32469 12.29028 12.25661 12.22404
## [585] 12.19295 12.16370 12.13667 12.11223 12.09076 12.07262 12.05457 12.03338
## [593] 12.00943 11.98310 11.95479 11.92486 11.89371 11.86171 11.82926 11.79672
## [601] 11.76450 11.73296 11.70250 11.67349 11.64633 11.62138 11.59904 11.57969
## [609] 11.56371 11.55148 11.54208 11.53425 11.52791 11.52298 11.51935 11.51695
## [617] 11.51569 11.51548 11.51624 11.51787 11.52028 11.52340 11.52713 11.53139
## [625] 11.53608 11.54112 11.54643 11.55191 11.55747 11.56304 11.56852 11.57382
## [633] 11.58042 11.58965 11.60125 11.61497 11.63054 11.64771 11.66620 11.68576
## [641] 11.70613 11.72704 11.74824 11.76946 11.79044 11.81092 11.83064 11.84934
## [649] 11.86675 11.88261 11.89667 11.91024 11.92477 11.94015 11.95628 11.97306
## [657] 11.99038 12.00815 12.02626 12.04462 12.06311 12.08165 12.10012 12.11842
## [665] 12.13646 12.15413 12.17134 12.18797 12.20393 12.21911 12.23342 12.24675
## [673] 12.25963 12.27262 12.28572 12.29889 12.31212 12.32539 12.33868 12.35197
## [681] 12.36523 12.37845 12.39160 12.40467 12.41764 12.43048 12.44317 12.45569
## [689] 12.46803 12.48016 12.49206 12.50372 12.51510 12.52632 12.53749 12.54860
## [697] 12.55965 12.57063 12.58154 12.59238 12.60314 12.61381 12.62440 12.63489
## [705] 12.64528 12.65556 12.66574 12.67580 12.68575 12.69557 12.70527 12.71483
## [713] 12.72425 12.73354 12.74265 12.75156 12.76029 12.76884 12.77723 12.78545
## [721] 12.79353 12.80147 12.80927 12.81695 12.82452 12.83198 12.83935 12.84663
## [729] 12.85383 12.86097
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.25, n = 730)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 12.63019 12.62540 12.62071 12.61612 12.61163 12.60724 12.60294 12.59873
##   [9] 12.59462 12.59059 12.58666 12.58281 12.57904 12.57536 12.57176 12.56824
##  [17] 12.56480 12.56144 12.55815 12.55493 12.55179 12.54872 12.54571 12.54277
##  [25] 12.53990 12.53709 12.53434 12.53165 12.52902 12.52645 12.52393 12.52146
##  [33] 12.51905 12.51669 12.51437 12.51210 12.50988 12.50768 12.50553 12.50342
##  [41] 12.50135 12.49933 12.49736 12.49545 12.49359 12.49179 12.49006 12.48839
##  [49] 12.48679 12.48526 12.48381 12.48243 12.48114 12.47993 12.47880 12.47777
##  [57] 12.47683 12.47598 12.47523 12.47459 12.47405 12.47362 12.47330 12.47309
##  [65] 12.47300 12.47303 12.47318 12.47346 12.47387 12.47440 12.47508 12.47589
##  [73] 12.47684 12.47794 12.47918 12.48058 12.48212 12.48382 12.48566 12.48762
##  [81] 12.48968 12.49186 12.49415 12.49654 12.49905 12.50166 12.50438 12.50720
##  [89] 12.51012 12.51314 12.51627 12.51949 12.52281 12.52622 12.52973 12.53333
##  [97] 12.53703 12.54081 12.54469 12.54865 12.55270 12.55683 12.56105 12.56535
## [105] 12.56973 12.57419 12.57873 12.58335 12.58804 12.59281 12.59765 12.60257
## [113] 12.60755 12.61260 12.61773 12.62291 12.62817 12.63348 12.63886 12.64431
## [121] 12.64996 12.65597 12.66229 12.66889 12.67572 12.68277 12.68998 12.69733
## [129] 12.70477 12.71228 12.71981 12.72733 12.73481 12.74221 12.74949 12.75662
## [137] 12.76356 12.77028 12.77673 12.78289 12.79023 12.80006 12.81214 12.82621
## [145] 12.84202 12.85931 12.87783 12.89732 12.91754 12.93823 12.95914 12.98001
## [153] 13.00058 13.02062 13.03985 13.05803 13.07490 13.09022 13.10372 13.11516
## [161] 13.12427 13.13289 13.14292 13.15422 13.16664 13.18006 13.19432 13.20930
## [169] 13.22485 13.24082 13.25709 13.27352 13.28995 13.30626 13.32231 13.33795
## [177] 13.35304 13.36745 13.38104 13.39366 13.40518 13.41546 13.42436 13.43173
## [185] 13.43745 13.44137 13.44334 13.44324 13.44092 13.43639 13.42984 13.42140
## [193] 13.41121 13.39941 13.38614 13.37153 13.35573 13.33886 13.32107 13.30249
## [201] 13.28326 13.26353 13.24341 13.22306 13.20261 13.18220 13.16196 13.14204
## [209] 13.12256 13.10367 13.08550 13.06820 13.04930 13.02656 13.00043 12.97133
## [217] 12.93972 12.90604 12.87072 12.83422 12.79696 12.75941 12.72199 12.68515
## [225] 12.64933 12.61498 12.58253 12.55243 12.52512 12.50104 12.48063 12.46164
## [233] 12.44160 12.42066 12.39896 12.37665 12.35386 12.33074 12.30744 12.28409
## [241] 12.26084 12.23784 12.21522 12.19313 12.17172 12.15112 12.13148 12.11294
## [249] 12.09565 12.07974 12.06537 12.05267 12.04206 12.03370 12.02740 12.02296
## [257] 12.02018 12.01887 12.01884 12.01987 12.02178 12.02437 12.02743 12.03078
## [265] 12.03422 12.03754 12.04056 12.04306 12.04486 12.04576 12.04557 12.04407
## [273] 12.04108 12.03839 12.03776 12.03898 12.04180 12.04602 12.05139 12.05770
## [281] 12.06471 12.07220 12.07994 12.08770 12.09527 12.10240 12.10887 12.11447
## [289] 12.11895 12.12209 12.12367 12.12346 12.12123 12.11675 12.11079 12.10429
## [297] 12.09729 12.08986 12.08203 12.07386 12.06540 12.05670 12.04780 12.03877
## [305] 12.02964 12.02046 12.01130 12.00219 11.99318 11.98434 11.97391 11.96037
## [313] 11.94404 11.92526 11.90437 11.88170 11.85759 11.83238 11.80639 11.77998
## [321] 11.75347 11.72720 11.70150 11.67672 11.65319 11.63124 11.61121 11.59343
## [329] 11.57825 11.56600 11.55701 11.54900 11.53960 11.52901 11.51745 11.50512
## [337] 11.49222 11.47898 11.46559 11.45226 11.43921 11.42664 11.41476 11.40377
## [345] 11.39388 11.38531 11.37826 11.37294 11.36956 11.36832 11.36944 11.37312
## [353] 11.37943 11.38821 11.39929 11.41251 11.42771 11.44474 11.46342 11.48360
## [361] 11.50512 11.52781 11.55152 11.57609 11.60134 11.62713 11.65330 11.67967
## [369] 11.70609 11.73240 11.75844 11.78404 11.80905 11.83330 11.85664 11.87890
## [377] 11.89992 11.91954 11.94068 11.96596 11.99478 12.02657 12.06073 12.09668
## [385] 12.13382 12.17156 12.20932 12.24651 12.28254 12.31682 12.34876 12.37777
## [393] 12.40326 12.42464 12.44460 12.46605 12.48879 12.51263 12.53736 12.56278
## [401] 12.58868 12.61487 12.64113 12.66727 12.69308 12.71837 12.74292 12.76654
## [409] 12.78902 12.81016 12.82976 12.84762 12.86352 12.87727 12.88868 12.89854
## [417] 12.90783 12.91653 12.92462 12.93211 12.93899 12.94524 12.95085 12.95583
## [425] 12.96016 12.96383 12.96683 12.96916 12.97080 12.97176 12.97201 12.97155
## [433] 12.97038 12.96848 12.96585 12.96248 12.95694 12.94805 12.93612 12.92146
## [441] 12.90438 12.88520 12.86423 12.84179 12.81818 12.79374 12.76876 12.74356
## [449] 12.71846 12.69378 12.66981 12.64689 12.62532 12.60541 12.58749 12.57186
## [457] 12.55884 12.54581 12.53016 12.51222 12.49234 12.47085 12.44808 12.42437
## [465] 12.40005 12.37547 12.35094 12.32682 12.30344 12.28112 12.26021 12.24104
## [473] 12.22395 12.20927 12.19734 12.18849 12.18093 12.17272 12.16398 12.15484
## [481] 12.14541 12.13581 12.12617 12.11661 12.10724 12.09820 12.08959 12.08154
## [489] 12.07418 12.06761 12.06197 12.05737 12.05393 12.05178 12.05104 12.05182
## [497] 12.05426 12.05878 12.06565 12.07466 12.08565 12.09841 12.11277 12.12853
## [505] 12.14552 12.16354 12.18241 12.20195 12.22195 12.24225 12.26266 12.28298
## [513] 12.30303 12.32263 12.34159 12.35972 12.37684 12.39275 12.40729 12.42025
## [521] 12.43416 12.45146 12.47185 12.49502 12.52066 12.54846 12.57813 12.60934
## [529] 12.64181 12.67521 12.70924 12.74359 12.77797 12.81205 12.84554 12.87813
## [537] 12.90950 12.93937 12.96740 12.99331 13.01678 13.03751 13.05519 13.06951
## [545] 13.08017 13.08686 13.09109 13.09457 13.09732 13.09932 13.10059 13.10111
## [553] 13.10089 13.09993 13.09823 13.09579 13.09261 13.08869 13.08403 13.07862
## [561] 13.07248 13.06560 13.05798 13.04963 13.04053 13.03069 13.02011 13.00726
## [569] 12.99081 12.97110 12.94846 12.92323 12.89574 12.86632 12.83532 12.80306
## [577] 12.76988 12.73611 12.70209 12.66815 12.63463 12.60185 12.57016 12.53989
## [585] 12.51137 12.48494 12.46093 12.43967 12.41779 12.39199 12.36269 12.33032
## [593] 12.29531 12.25811 12.21913 12.17882 12.13760 12.09591 12.05417 12.01283
## [601] 11.97230 11.93304 11.89546 11.85999 11.82708 11.79715 11.77063 11.74797
## [609] 11.72957 11.71350 11.69752 11.68171 11.66613 11.65085 11.63594 11.62145
## [617] 11.60746 11.59404 11.58124 11.56913 11.55778 11.54726 11.53762 11.52895
## [625] 11.52129 11.51472 11.50931 11.50511 11.50220 11.50064 11.50136 11.50501
## [633] 11.51129 11.51986 11.53041 11.54261 11.55613 11.57066 11.58588 11.60146
## [641] 11.61707 11.63241 11.64713 11.66093 11.67347 11.68444 11.69569 11.70914
## [649] 11.72459 11.74183 11.76064 11.78081 11.80213 11.82440 11.84740 11.87092
## [657] 11.89475 11.91868 11.94250 11.96600 11.98896 12.01118 12.03245 12.05256
## [665] 12.07129 12.08843 12.10378 12.11839 12.13345 12.14891 12.16475 12.18093
## [673] 12.19742 12.21419 12.23121 12.24844 12.26584 12.28339 12.30106 12.31880
## [681] 12.33659 12.35440 12.37219 12.38993 12.40758 12.42512 12.44250 12.45970
## [689] 12.47669 12.49343 12.50989 12.52603 12.54183 12.55749 12.57324 12.58907
## [697] 12.60497 12.62092 12.63692 12.65296 12.66902 12.68509 12.70116 12.71723
## [705] 12.73327 12.74928 12.76524 12.78115 12.79700 12.81277 12.82845 12.84403
## [713] 12.85950 12.87484 12.88998 12.90486 12.91948 12.93389 12.94810 12.96213
## [721] 12.97601 12.98977 13.00342 13.01699 13.03051 13.04399 13.05747 13.07096
## [729] 13.08448 13.09807
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.25, n = 730)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 12.01849 12.01368 12.00896 12.00432 11.99976 11.99527 11.99086 11.98651
##   [9] 11.98224 11.97803 11.97388 11.96979 11.96575 11.96177 11.95784 11.95396
##  [17] 11.95012 11.94632 11.94257 11.93885 11.93516 11.93150 11.92787 11.92427
##  [25] 11.92069 11.91713 11.91358 11.91005 11.90653 11.90302 11.89951 11.89600
##  [33] 11.89250 11.88899 11.88547 11.88195 11.87841 11.87486 11.87129 11.86770
##  [41] 11.86409 11.86046 11.85679 11.85308 11.84933 11.84554 11.84171 11.83786
##  [49] 11.83399 11.83011 11.82623 11.82234 11.81847 11.81461 11.81077 11.80696
##  [57] 11.80319 11.79946 11.79578 11.79216 11.78860 11.78512 11.78170 11.77838
##  [65] 11.77514 11.77201 11.76897 11.76605 11.76325 11.76057 11.75803 11.75562
##  [73] 11.75336 11.75126 11.74931 11.74753 11.74592 11.74449 11.74325 11.74221
##  [81] 11.74136 11.74072 11.74030 11.74010 11.74013 11.74017 11.74004 11.73975
##  [89] 11.73932 11.73876 11.73810 11.73735 11.73654 11.73567 11.73477 11.73387
##  [97] 11.73296 11.73208 11.73125 11.73048 11.72978 11.72919 11.72871 11.72837
## [105] 11.72818 11.72816 11.72834 11.72872 11.72933 11.73019 11.73132 11.73272
## [113] 11.73443 11.73646 11.73883 11.74155 11.74466 11.74815 11.75206 11.75640
## [121] 11.76119 11.76645 11.77219 11.77844 11.78522 11.79253 11.80139 11.81259
## [129] 11.82584 11.84086 11.85734 11.87500 11.89355 11.91271 11.93218 11.95167
## [137] 11.97090 11.98957 12.00739 12.02407 12.03934 12.05288 12.06728 12.08507
## [145] 12.10591 12.12944 12.15534 12.18324 12.21282 12.24372 12.27560 12.30812
## [153] 12.34093 12.37369 12.40605 12.43768 12.46823 12.49734 12.52469 12.54993
## [161] 12.57270 12.59267 12.60950 12.62514 12.64170 12.65910 12.67722 12.69597
## [169] 12.71526 12.73498 12.75502 12.77530 12.79572 12.81616 12.83654 12.85676
## [177] 12.87671 12.89629 12.91541 12.93397 12.95186 12.96899 12.98526 13.00057
## [185] 13.01482 13.02791 13.03973 13.05020 13.05921 13.06666 13.07245 13.07648
## [193] 13.07866 13.07888 13.07705 13.07306 13.06619 13.05597 13.04269 13.02664
## [201] 13.00810 12.98737 12.96473 12.94046 12.91486 12.88821 12.86080 12.83292
## [209] 12.80485 12.77689 12.74931 12.72241 12.69648 12.67180 12.64866 12.62735
## [217] 12.60816 12.58729 12.56124 12.53075 12.49653 12.45931 12.41982 12.37878
## [225] 12.33692 12.29496 12.25362 12.21364 12.17573 12.14062 12.10903 12.08170
## [233] 12.05935 12.03935 12.01869 11.99746 11.97576 11.95368 11.93132 11.90879
## [241] 11.88616 11.86356 11.84106 11.81877 11.79679 11.77522 11.75414 11.73366
## [249] 11.71388 11.69489 11.67679 11.65967 11.64365 11.62880 11.61549 11.60392
## [257] 11.59392 11.58536 11.57809 11.57195 11.56679 11.56248 11.55885 11.55576
## [265] 11.55307 11.55062 11.54826 11.54584 11.54323 11.54026 11.53678 11.53266
## [273] 11.52774 11.52187 11.51490 11.50822 11.50321 11.49970 11.49750 11.49646
## [281] 11.49640 11.49715 11.49855 11.50041 11.50257 11.50486 11.50711 11.50915
## [289] 11.51080 11.51190 11.51227 11.51175 11.51016 11.50734 11.50311 11.49730
## [297] 11.48979 11.48070 11.47020 11.45848 11.44572 11.43210 11.41779 11.40297
## [305] 11.38783 11.37255 11.35730 11.34226 11.32762 11.31355 11.30024 11.28786
## [313] 11.27659 11.26661 11.25810 11.24897 11.23720 11.22308 11.20691 11.18899
## [321] 11.16963 11.14910 11.12773 11.10579 11.08359 11.06143 11.03961 11.01842
## [329] 10.99817 10.97914 10.96164 10.94597 10.93242 10.92129 10.91288 10.90749
## [337] 10.90542 10.90592 10.90801 10.91158 10.91652 10.92275 10.93016 10.93865
## [345] 10.94811 10.95846 10.96958 10.98137 10.99375 11.00659 11.01982 11.03331
## [353] 11.04699 11.06073 11.07445 11.08804 11.10140 11.11598 11.13314 11.15267
## [361] 11.17435 11.19797 11.22331 11.25015 11.27829 11.30750 11.33756 11.36827
## [369] 11.39940 11.43074 11.46208 11.49319 11.52387 11.55390 11.58305 11.61112
## [377] 11.63789 11.66315 11.68667 11.70824 11.72989 11.75362 11.77914 11.80620
## [385] 11.83453 11.86386 11.89392 11.92444 11.95516 11.98581 12.01611 12.04581
## [393] 12.07463 12.10230 12.12856 12.15314 12.17577 12.19619 12.21412 12.23110
## [401] 12.24878 12.26702 12.28570 12.30471 12.32391 12.34319 12.36242 12.38147
## [409] 12.40023 12.41857 12.43637 12.45351 12.46985 12.48528 12.49968 12.51292
## [417] 12.52488 12.53543 12.54446 12.55183 12.55743 12.56128 12.56353 12.56430
## [425] 12.56373 12.56194 12.55907 12.55525 12.55061 12.54528 12.53940 12.53309
## [433] 12.52648 12.51972 12.51292 12.50622 12.49976 12.49365 12.48805 12.48306
## [441] 12.47884 12.47400 12.46722 12.45868 12.44855 12.43703 12.42429 12.41051
## [449] 12.39588 12.38058 12.36478 12.34868 12.33245 12.31627 12.30032 12.28480
## [457] 12.26987 12.25573 12.24254 12.23050 12.21979 12.21058 12.20124 12.19016
## [465] 12.17760 12.16382 12.14909 12.13365 12.11777 12.10170 12.08571 12.07005
## [473] 12.05498 12.04076 12.02765 12.01590 12.00578 11.99754 11.98979 11.98109
## [481] 11.97157 11.96138 11.95065 11.93953 11.92817 11.91669 11.90526 11.89400
## [489] 11.88306 11.87258 11.86271 11.85359 11.84535 11.83815 11.83212 11.82741
## [497] 11.82415 11.82250 11.82259 11.82434 11.82751 11.83202 11.83777 11.84466
## [505] 11.85262 11.86153 11.87132 11.88189 11.89315 11.90501 11.91737 11.93014
## [513] 11.94323 11.95655 11.97001 11.98351 11.99696 12.01028 12.02336 12.03611
## [521] 12.04846 12.06029 12.07152 12.08206 12.09182 12.10235 12.11513 12.12998
## [529] 12.14670 12.16510 12.18499 12.20617 12.22847 12.25167 12.27560 12.30006
## [537] 12.32485 12.34979 12.37469 12.39934 12.42357 12.44717 12.46996 12.49175
## [545] 12.51233 12.53153 12.54915 12.56499 12.57887 12.59059 12.59996 12.60679
## [553] 12.61089 12.61342 12.61566 12.61758 12.61918 12.62042 12.62129 12.62177
## [561] 12.62182 12.62144 12.62059 12.61927 12.61744 12.61508 12.61218 12.60872
## [569] 12.60466 12.59891 12.59051 12.57971 12.56674 12.55184 12.53526 12.51723
## [577] 12.49800 12.47779 12.45685 12.43542 12.41373 12.39203 12.37056 12.34955
## [585] 12.32924 12.30988 12.29169 12.27492 12.25982 12.24661 12.23247 12.21470
## [593] 12.19370 12.16990 12.14369 12.11549 12.08572 12.05478 12.02308 11.99104
## [601] 11.95906 11.92756 11.89696 11.86765 11.84005 11.81458 11.79165 11.77166
## [609] 11.75502 11.74216 11.73160 11.72162 11.71220 11.70335 11.69507 11.68735
## [617] 11.68019 11.67360 11.66756 11.66207 11.65714 11.65275 11.64892 11.64563
## [625] 11.64289 11.64068 11.63902 11.63790 11.63731 11.63725 11.63773 11.63874
## [633] 11.64122 11.64597 11.65277 11.66140 11.67163 11.68325 11.69602 11.70972
## [641] 11.72414 11.73905 11.75423 11.76944 11.78448 11.79912 11.81313 11.82629
## [649] 11.83838 11.84918 11.85845 11.86736 11.87711 11.88763 11.89883 11.91061
## [657] 11.92289 11.93558 11.94859 11.96184 11.97522 11.98866 12.00207 12.01535
## [665] 12.02842 12.04119 12.05357 12.06548 12.07682 12.08750 12.09743 12.10654
## [673] 12.11529 12.12420 12.13326 12.14244 12.15172 12.16108 12.17050 12.17996
## [681] 12.18943 12.19890 12.20835 12.21774 12.22707 12.23631 12.24544 12.25444
## [689] 12.26329 12.27196 12.28044 12.28870 12.29672 12.30462 12.31251 12.32040
## [697] 12.32827 12.33613 12.34395 12.35174 12.35948 12.36718 12.37482 12.38240
## [705] 12.38991 12.39735 12.40471 12.41198 12.41915 12.42622 12.43319 12.44004
## [713] 12.44677 12.45337 12.45980 12.46604 12.47209 12.47797 12.48369 12.48926
## [721] 12.49470 12.50001 12.50520 12.51030 12.51530 12.52023 12.52509 12.52989
## [729] 12.53466 12.53939
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")